﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using MetroFramework.Forms;

namespace Serverslist
{
    public partial class Form1 : MetroForm
    {
       
        // i can turn all of it oop by using class ... 
            
        public Form1()
        {
            InitializeComponent();   
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            search("select * from ServerList");
        }

        private void addserver_Click(object sender, EventArgs e)
        {
            string msg = "Server Add Succefully !";
            string procces = string.Format( "Insert into ServerList values ('{0}','{1}','{2}')", serverlink.Text, Flag.Text, speed.Text); 
            Dataproccess(procces,msg);
        }
        private void Dataproccess(string proc, string msg)
        {

            // 0. DataBase Variables .
            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();

            //1. requirements .
            conn.ConnectionString = "Provider=SQLNCLI11;Data Source=DODY\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=TravianServers1";
            cmd.Connection = conn;
            cmd.CommandText = string.Format(proc);


            // 2. DbWork.
            try
            {
                conn.Open();
                var x = cmd.ExecuteNonQuery();
                MessageBox.Show(msg);
                conn.Close();
            }
            catch (OleDbException ex)
            {

                string exx = ex.ToString();
                MessageBox.Show("Something Wrong .");


            }
        
        
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            // statement for Deleted 
            // delete from serverlist where ServerUrl='http://tx3.travian.ae'
            string msg = "Server Was Deleted !";
            string procces = "delete from ServerList Where ServerID ='" + serverid.Text+ "'";
            Dataproccess(procces, msg);
        }

        private void metroButton3_Click(object sender, EventArgs e)
        {
            string msg = "Server Updated Succefully !";
            string procces = "update ServerList set ServerUrl ='" + serverlink.Text + "'" +"," + "Flag=" + "'" + Flag.Text + "'"  + "," + "Speed ="+"'" + speed.Text + "'";
            string procces1 = "update ServerList set ServerUrl ='" + updateid.Text + "'";
            if (serverlink.Text != null || Flag.Text != null || speed.Text != null)
            {
                Dataproccess(procces, msg);
            }
            else
            {


                Dataproccess(procces1, msg);
            
            }
        }

        private void metroButton1_Click(object sender, EventArgs e)
        {

        }
        private void search(string query) 
        {

            // 0. DataBase Variables .
            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();
            DataTable table = new DataTable();
            string command_find = query;
            //1. requirements .
            conn.ConnectionString = "Provider=SQLNCLI11;Data Source=DODY\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=TravianServers1";
            cmd.Connection = conn;
            cmd.CommandText = command_find;


            // 2. DbWork.
            try
            {
                conn.Open();
                table.Load( cmd.ExecuteReader());
                dgv.DataSource = table;
                conn.Close();
            }
            catch (OleDbException ex)
            {

                string exx = ex.ToString();
                MessageBox.Show("Something Wrong .");


            }
        
        
        }

        private void ssearch_Click(object sender, EventArgs e)
        {
            search("select * from ServerList Where Flag like '%" + Flag.Text + "%' ");
        }

        private void form2_Click(object sender, EventArgs e)
        {
            Download_Server show = new Download_Server();
            show.Show();
        }
    }
}
